package com.dongdongshop.service.impl;

import com.dongdongshop.api.AdminServiceApi;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.dto.TbGoodsDescDTO;
import com.dongdongshop.domain.model.TbGoods;
import com.dongdongshop.domain.model.TbGoodsDesc;
import com.dongdongshop.domain.model.TbItem;
import com.dongdongshop.domain.model.TbItemExample;
import com.dongdongshop.domain.vo.GoodsDescVO;
import com.dongdongshop.domain.vo.GoodsVO;
import com.dongdongshop.domain.vo.ItemVO;
import com.dongdongshop.dto.ItemCatDTO;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.service.GoodsDescService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.TbItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper goodsMapper;
    @Autowired
    private GoodsDescService descService;
    @Autowired
    private TbItemService itemService;
    @Autowired
    private AdminServiceApi adminServiceApi;
    @Autowired
    private TbGoodsDescMapper goodsDescMapper;

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public Result addGoods(GoodsVO goodsVo) {
        TbGoods goods = new TbGoods();
        BeanUtils.copyProperties(goodsVo,goods);
        //添加商家并返回主键id
        goodsMapper.addGoods(goods);
        TbGoodsDescDTO goodsDescDTO = new TbGoodsDescDTO();
        BeanUtils.copyProperties(goodsVo,goodsDescDTO);
        //取出商家主键为商品明细表赋值
        goodsDescDTO.setGoodsId(goods.getId());
        //业务层之间传递数据
        descService.addGoods(goodsDescDTO);
        //取出商品表，sku中的数据
        List<ItemVO> itemVoList = goodsVo.getItemVoList();
        for (ItemVO tbItemVo : itemVoList) {
            tbItemVo.setGoodsId(goods.getId());
            tbItemVo.setSellerId(goods.getSellerId());
            tbItemVo.setCreateTime(new Date());
            tbItemVo.setUpdateTime(new Date());
        }
        //批量增加商品表，sku中的数据
        itemService.addItemManys(itemVoList);

        return Result.OK();
    }

    @Override
    public GoodsVO getGoodsById(Long goodsId) {
        TbGoods tbGoods = goodsMapper.selectByPrimaryKey(goodsId);
        GoodsVO vo = new GoodsVO();
        BeanUtils.copyProperties(tbGoods,vo);
        //三级联动
        Result<ItemCatDTO> itemCatDTOResult1 = adminServiceApi.selectById(vo.getCategory1Id());
        Result<ItemCatDTO> itemCatDTOResult2 = adminServiceApi.selectById(vo.getCategory2Id());
        Result<ItemCatDTO> itemCatDTOResult3 = adminServiceApi.selectById(vo.getCategory3Id());
        if (itemCatDTOResult1.getData() != null && itemCatDTOResult1.getCode() == 0){
            vo.setCategory1Name(itemCatDTOResult1.getData().getName());
        }
        if (itemCatDTOResult2.getData() != null && itemCatDTOResult2.getCode() == 0){
            vo.setCategory2Name(itemCatDTOResult2.getData().getName());
        }
        if (itemCatDTOResult3.getData() != null && itemCatDTOResult3.getCode() == 0){
            vo.setCategory3Name(itemCatDTOResult3.getData().getName());
        }
        //查询goods_desc详情表
        TbGoodsDesc tbGoodsDesc = goodsDescMapper.selectByPrimaryKey(goodsId);
        GoodsDescVO goodsDescVO = new GoodsDescVO();
        BeanUtils.copyProperties(tbGoodsDesc,goodsDescVO);
        //将goods_desc详情表中的数据set到goods表中
        vo.setGoodsDescVO(goodsDescVO);
        //规格的sku
        TbItemExample example = new TbItemExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId);
        example.setOrderByClause("is_default desc");
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        List<ItemVO> itemVOList = tbItems.stream().map(tbItem -> {
            ItemVO itemVO = new ItemVO();
            BeanUtils.copyProperties(tbItem, itemVO);
            return itemVO;
        }).collect(Collectors.toList());
        vo.setItemVoList(itemVOList);
        return vo;
    }
}
